package com.zuxia.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zuxia.dao.IRepairDao;
import com.zuxia.entity.Repair;
@Component
public class RepairDao implements IRepairDao{
	@Autowired
	private SessionFactory sf;
	@Override
	public List findRepair(int pageNow, String xname, int pageSize) {
		Session session = sf.getCurrentSession();
		Criteria criteria = null;
		if(xname != null || xname != ""){
			criteria = session.createCriteria(Repair.class);
			criteria .add(Restrictions.like("repairName", "%"+xname+"%"));
		}else{
			criteria = session.createCriteria(Repair.class);
		}
		criteria.setFirstResult((pageNow - 1) * pageSize);
		criteria.setMaxResults(pageSize);
		List list = criteria.list();
		return list;
		
	}

	@Override
	public int repsize() {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.createCriteria(Repair.class).list().size();
	}

	@Override
	public void delrep(Repair repair) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.delete(repair);
		tran.commit();
		session.close();
	}

	@Override
	public void addrep(Repair repair) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.save(repair);
		tran.commit();
		session.close();
	}

	@Override
	public Repair findupdateRepair(Repair repair) {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.get(Repair.class, repair.getRepairId());
	}

	@Override
	public void updrep(Repair repair) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.update(repair);
		tran.commit();
		session.close();
	}

}
